package JackAnalyzer;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;


public class JackData {
	
	public static final int KEYWORD = 1;
	public static final int SYMBOL = 2;
	public static final int IDENTIFIER = 3;
	public static final int INT_CONST = 4;
	public static final int STRING_CONST = 5;

	public static final Map<String, Integer> Tokens;
    static {
        Map<String, Integer> aMap = new HashMap<String, Integer>();
        aMap.put("class", KEYWORD);
        aMap.put("constructor", KEYWORD);
        aMap.put("function", KEYWORD);
        aMap.put("method", KEYWORD);
        aMap.put("field", KEYWORD);
        aMap.put("static", KEYWORD);
        aMap.put("var", KEYWORD);
        aMap.put("int", KEYWORD);
        aMap.put("char", KEYWORD);
        aMap.put("boolean", KEYWORD);
        aMap.put("void", KEYWORD);
        aMap.put("true", KEYWORD);
        aMap.put("false", KEYWORD);
        aMap.put("null", KEYWORD);
        aMap.put("this", KEYWORD);
        aMap.put("let", KEYWORD);
        aMap.put("do", KEYWORD);
        aMap.put("if", KEYWORD);
        aMap.put("else", KEYWORD);
        aMap.put("while", KEYWORD);
        aMap.put("return", KEYWORD);

        aMap.put("{", SYMBOL);
        aMap.put("}", SYMBOL);
        aMap.put("(", SYMBOL);
        aMap.put(")", SYMBOL);
        aMap.put("[", SYMBOL);
        aMap.put("]", SYMBOL);
        aMap.put(".", SYMBOL);
        aMap.put(",", SYMBOL);
        aMap.put(";", SYMBOL);
        aMap.put("+", SYMBOL);
        aMap.put("-", SYMBOL);
        aMap.put("*", SYMBOL);
        aMap.put("/", SYMBOL);     
        aMap.put("&", SYMBOL);
        aMap.put("|", SYMBOL);
        aMap.put("<", SYMBOL);
        aMap.put(">", SYMBOL);
        aMap.put("=", SYMBOL);
        aMap.put("~", SYMBOL);
        
        Tokens = Collections.unmodifiableMap(aMap);
    }
    
    public static boolean isSubRoutine(int type , String val){
    	if(type == KEYWORD){
    		if(val.contentEquals("constructor") ||
    		   val.contentEquals("function") ||
    		   val.contentEquals("method")){
    			return true;
    		}
    	}
    	return false;
    }
    
    public static boolean isClassVar(int type , String val){
    	if(type == KEYWORD){
    		if(val.contentEquals("field") ||
    		   val.contentEquals("static")){
    			return true;
    		}
    	}
    	return false;
    }
    
    public static boolean isVar(int type , String val){
    	if(type == KEYWORD){
    		if(val.contentEquals("var")){
    			return true;
    		}
    	}
    	return false;
    }
    
    public static boolean isStatement(int type , String val){
    	if(type == KEYWORD){
    		if(val.contentEquals("do") ||
    		   val.contentEquals("let") ||	
    		   val.contentEquals("while") ||
    		   val.contentEquals("return") ||
    		   val.contentEquals("do") ||
    		   val.contentEquals("if")){
    			return true;
    		}
    	}
    	return false;
    }

	public static boolean isKeywordConstant(int tokenType, String keyWord2) {
		if(tokenType == KEYWORD){
			if(keyWord2.contentEquals("true") || 
			   keyWord2.contentEquals("false") ||
			   keyWord2.contentEquals("null") ||
			   keyWord2.contentEquals("this")){
				return true;
			}
		}
		return false;
	}
	
	
	public static boolean isTerm(int tokenType, String keyWord2) {
		if(tokenType == INT_CONST || 
		   tokenType == STRING_CONST ||
		   isKeywordConstant(tokenType , keyWord2) ||
		   tokenType == IDENTIFIER || 
		   keyWord2 == "(" ){
			return true;
		}
		return false;
	}
	public static String xmlSymbol(String s){
		switch (s.charAt(0)) {
		case '<': return "&lt;";
		case '>': return "&gt;";
		case '&': return "&amp;";
		case '\'': return "&apos;";
		case '\"': return "&quot;";
		}
		return s;
	}
	
	public static boolean returnValue(String val){
		return val.contentEquals("int") || val.contentEquals("char") ||
			   val.contentEquals("boolean") || val.contentEquals("void") ; 
	}
	
	public static boolean isInt(String s){
		try{
			Integer.parseInt(s);
			return true;
		}catch(NumberFormatException e){
			return false;
		}
	}

	public static boolean isStringConst(String s){
//		if(s.contains("Level"))
//			System.err.println(s);
		if(s.charAt(0) == '"' && s.charAt(s.length()-1) == '"')
			return true;
		return false;
	}
}